import os

import datahub.emitter.mce_builder as builder
import datahub.metadata.schema_classes as models
from datahub.emitter.mcp import MetadataChangeProposalWrapper
from datahub.emitter.rest_emitter import DatahubRestEmitter

gms_server = os.getenv("DATAHUB_GMS_URL", "http://localhost:8080")
token = os.getenv("DATAHUB_GMS_TOKEN")
emitter = DatahubRestEmitter(gms_server=gms_server, token=token)

features = [
    {
        "name": "user_country",
        "description": "Country of user residence",
        "data_type": "NOMINAL",
    },
    {
        "name": "subscription_tier",
        "description": "User subscription level: free, basic, premium, enterprise",
        "data_type": "ORDINAL",
    },
    {
        "name": "is_email_verified",
        "description": "Whether the user has verified their email address",
        "data_type": "BINARY",
    },
    {
        "name": "total_purchases",
        "description": "Total number of purchases made by the user",
        "data_type": "COUNT",
    },
    {
        "name": "signup_hour",
        "description": "Hour of day when user signed up (0-23)",
        "data_type": "TIME",
    },
    {
        "name": "lifetime_value",
        "description": "Total revenue generated by user in USD",
        "data_type": "CONTINUOUS",
    },
    {
        "name": "user_bio",
        "description": "User profile biography text",
        "data_type": "TEXT",
    },
]

for feature_def in features:
    feature_urn = builder.make_ml_feature_urn(
        feature_table_name="user_features",
        feature_name=feature_def["name"],
    )

    metadata_change_proposal = MetadataChangeProposalWrapper(
        entityUrn=feature_urn,
        aspect=models.MLFeaturePropertiesClass(
            description=feature_def["description"],
            dataType=feature_def["data_type"],
        ),
    )

    emitter.emit(metadata_change_proposal)
